package com.yanchi.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 自定义Realm
 */
public class CustomRealm extends AuthorizingRealm {

    // 授权方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    // 认证方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        Object principal =  token.getPrincipal();
        if ("yanchi".equals(principal)){
            // 参数一：数据库中用户账号
            // 参数二：数据库中用户密码
            // 参数三：提供当前realm的名字
            return new SimpleAuthenticationInfo(principal,"123456",this.getName());
        }
        return null;
    }
}
